package com.retail.system.client.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.retail.core.entity.result.Result;
import com.retail.system.client.AdminClient;
import com.retail.system.entity.vo.AdminVo;
import com.retail.system.entity.vo.SimpleTreeVo;
import com.retail.system.entity.vo.TableTreeVo;
import com.retail.system.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminClientImpl implements AdminClient {

    @Autowired
    private AdminService adminService;

    @Override
    public Result<List<AdminVo>> adminGroupTableTree(Long adminId) {
        List<AdminVo> chilidAdmin = adminService.childAdminByParentIdAndId(adminId);
        return Result.success(chilidAdmin);
    }

    @Override
    public Result<List<SimpleTreeVo>> adminSimpleTree(Long adminId, Integer level) {
        SimpleTreeVo simpleTreeVo = recursionSimpleTree(adminId, level);
        List<SimpleTreeVo> ret = new ArrayList<>();
        ret.add(simpleTreeVo);
        return Result.success(ret);
    }


    private SimpleTreeVo recursionSimpleTree(Long adminId, Integer level) {
        AdminVo adminVo = adminService.getModel(adminId);
        SimpleTreeVo simpleTreeVo = converTree(adminVo);
        if (level > 0) {
            Long adminId1 = adminVo.getId();
            List<SimpleTreeVo> ret = new ArrayList<>();
            List<AdminVo> childAdmin = adminService.childAdminByParentId(adminId1);
            for (AdminVo ca : childAdmin) {
                ret.add(recursionSimpleTree(ca.getId(), level--));
            }
            simpleTreeVo.setChildren(ret);
        }
        return simpleTreeVo;
    }


    private SimpleTreeVo converTree(AdminVo adminVo) {
        SimpleTreeVo simpleTreeVo = new SimpleTreeVo();
        simpleTreeVo.setValue(adminVo.getId() + "");
        simpleTreeVo.setLabel(adminVo.getName());
        return simpleTreeVo;
    }

    private TableTreeVo convertTableTree(AdminVo adminVo) {
        TableTreeVo tableTreeVo = new TableTreeVo();
        tableTreeVo.setId(adminVo.getId());
        tableTreeVo.setData(adminVo);
        return tableTreeVo;
    }
}
